package com.wuh.config;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;

@Configuration
public class DruidConfig  implements WebMvcConfigurer {
	

	@ConfigurationProperties(prefix = "spring.datasource")
	@Bean
	public DataSource druid() {
		return new DruidDataSource();
	}
	
	// 配置Druid监控
	@Bean
	public ServletRegistrationBean<StatViewServlet> statViewServlet() {
		ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<StatViewServlet>(new StatViewServlet(), "/druid/*");
		Map<String, String> initParameters = new HashMap<String, String>();
		// 配置访问druid监控的用户名及密码
		initParameters.put("loginUsername", "admin");
		initParameters.put("loginPassword", "123123");
		initParameters.put("allow", ""); // 默认允许所有的访问
		bean.setInitParameters(initParameters);
		return bean;
	}
	
	// 配置一个web监控的Filter
	/**
	 * @return
	 */
	@Bean
	public FilterRegistrationBean<WebStatFilter> webStatFilter() {
		FilterRegistrationBean<WebStatFilter> filter= new FilterRegistrationBean<WebStatFilter>();
		// 设置拦截器
		filter.setFilter(new WebStatFilter());
		Map<String, String> initParameters = new HashMap<String, String>();
		// 排除掉一些资源的拦截，以及druid
		initParameters.put("exclusions", "*.js,*.css,*.png,*.jpg,/druid/*");
		filter.setInitParameters(initParameters);
		// 配置拦截请求
		filter.setUrlPatterns(Arrays.asList("/*"));
		return filter;
	}


	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/user/img/**").addResourceLocations("file:C:\\Users\\sheng156\\git\\toefl\\src\\main\\resources\\static\\user\\img\\");
		registry.addResourceHandler("/teacher/img/**").addResourceLocations("file:C:\\Users\\sheng156\\git\\toefl\\src\\main\\resources\\static\\teacher\\img\\");
		registry.addResourceHandler("/active/img/**").addResourceLocations("file:E:\\GitWarehouse\\toefl\\src\\main\\resources\\static\\active\\img\\");
		registry.addResourceHandler("/classimage/**").addResourceLocations("file:E:\\GitWarehouse\\toefl\\src\\main\\resources\\static\\classimage\\");
		registry.addResourceHandler("/classAttachment/**").addResourceLocations("file:E:\\GitWarehouse\\toefl\\src\\main\\resources\\static\\classAttachment\\");
        registry.addResourceHandler("/toeflImg/**").addResourceLocations("file:C:\\Users\\Administrator\\git\\toefl\\src\\main\\resources\\static\\toeflImg\\");
	}
}
